/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import POJO.CongThuc;
import UTIL.HibernateUtil;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author ChanhTin
 */
public class CongThucDAO {
     
    /**
     * @desc lay danh sach cong thuc
     * @return 
     */
    public static List<CongThuc> layDanhSach() {
        List<CongThuc> ds = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            String hql = "select ct from CongThuc ct";
            org.hibernate.Query query = session.createQuery(hql);
            ds = query.list();
        } catch (HibernateException ex) {
            System.err.println(ex);
        } finally {
            session.close();
        }
        return ds;  
    }
    
    /**
     * @desc tim cong thuc theo ma cong thuc
     * @param maCongThuc
     * @return 
     */
    public static CongThuc layThongTinCongThuc(int maCongThuc) {
        CongThuc ct = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            ct = (CongThuc) session.get(CongThuc.class, maCongThuc);
        } catch (HibernateException ex) {
            System.err.println(ex);
        } finally {
            session.close();
        }
        return ct;
    }
    
    /**
     * @desc them cong thuc
     * @param ct
     * @return 
     */
    public static boolean themCongThuc(CongThuc ct) throws HibernateException{
        Session session = HibernateUtil.getSessionFactory().openSession(); 
        if (CongThucDAO.layThongTinCongThuc(ct.getMaCongThuc())!=null) { 
            return false; 
        }
        boolean flag = true;
        Transaction transaction = null; 
        try { 
            transaction = session.beginTransaction(); 
            session.save(ct); 
            transaction.commit(); 
        } catch (HibernateException ex) { 
            //Log the exception 
            transaction.rollback(); 
            //System.err.println(ex);
            flag = false;
        } finally { 
            session.close(); 
        } 
        return flag;
    }
    
    /**
     * @desc cap nhat thong tin cong thuc
     * @param ct
     * @return 
     */
    public static boolean capNhatThongTinCongThuc(CongThuc ct) throws HibernateException{ 
        Session session = HibernateUtil.getSessionFactory().openSession(); 
        if (CongThucDAO.layThongTinCongThuc(ct.getMaCongThuc()) == null) { 
            return false; 
        }
        boolean flag = true;
        Transaction transaction = null; 
        try { 
            transaction = session.beginTransaction(); 
            session.update(ct); 
            transaction.commit(); 
        } catch (HibernateException ex) { 
            //Log the exception 
            transaction.rollback(); 
           //System.err.println(ex);
            flag = false;
        } finally { 
            session.close(); 
        } 
        return flag; 
    } 
    
    /**
     * @desc xoa cong thuc
     * @param maCongThuc
     * @return 
     */
    public static boolean xoaCongThuc(int maCongThuc) throws HibernateException{ 
        Session session = HibernateUtil.getSessionFactory().openSession(); 
        CongThuc ct = CongThucDAO.layThongTinCongThuc(maCongThuc); 
        if(ct==null){ 
            return false; 
        }
        boolean flag = true;
        Transaction transaction = null; 
        try { 
            transaction = session.beginTransaction(); 
            session.delete(ct); 
            transaction.commit(); 
        } catch (HibernateException ex) { 
            //Log the exception 
            transaction.rollback(); 
            //System.err.println(ex);
            flag = false;
        } finally { 
            session.close(); 
        } 
        return flag; 
    }
}
